<template>
  <div class="customer-detail">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>客户详情</span>
          <div>
            <el-button @click="goBack">返回</el-button>
            <el-button type="primary" @click="handleEdit">编辑</el-button>
          </div>
        </div>
      </template>

      <el-descriptions :column="1" border>
        <el-descriptions-item label="客户ID">{{ customerDetail.id }}</el-descriptions-item>
        <el-descriptions-item label="客户名称">{{ customerDetail.name }}</el-descriptions-item>
        <el-descriptions-item label="联系人">{{ customerDetail.contact }}</el-descriptions-item>
        <el-descriptions-item label="联系电话">{{ customerDetail.phone }}</el-descriptions-item>
        <el-descriptions-item label="地址">{{ customerDetail.address }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ formatDate(customerDetail.createTime) }}</el-descriptions-item>
      </el-descriptions>

      <el-divider />

      <h3>关联业务</h3>
      <el-table :data="relatedBusiness" border style="width: 100%">
        <el-table-column prop="id" label="业务ID" width="100" />
        <el-table-column prop="type" label="业务类型" />
        <el-table-column prop="content" label="业务内容" show-overflow-tooltip />
        <el-table-column prop="createTime" label="创建时间" width="180">
          <template #default="{ row }">
            {{ formatDate(row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template #default="{ row }">
            <el-button link type="primary" @click="viewBusiness(row.id)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getCustomerDetail } from '@/api/customer'
import { getBusinessList } from '@/api/business'
import { formatDate } from '@/utils/date'

const route = useRoute()
const router = useRouter()

const customerId = ref(route.params.id)
const customerDetail = ref({})
const relatedBusiness = ref([])
const loading = ref(false)

const fetchCustomerDetail = async () => {
  try {
    loading.value = true
    const res = await getCustomerDetail(customerId.value)
    customerDetail.value = res
    fetchRelatedBusiness()
  } catch (error) {
    ElMessage.error('获取客户详情失败: ' + error.message)
    goBack()
  } finally {
    loading.value = false
  }
}

const fetchRelatedBusiness = async () => {
  try {
    const res = await getBusinessList({ customerId: customerId.value, size: 100 })
    relatedBusiness.value = res.data
  } catch (error) {
    console.error('获取关联业务失败:', error)
  }
}

const handleEdit = () => {
  router.push({ name: 'CustomerDetailEdit', params: { id: customerId.value } })
}

const viewBusiness = (id) => {
  router.push({ name: 'BusinessDetail', params: { id } })
}

const goBack = () => {
  router.push({ name: 'CustomerList' })
}

onMounted(() => {
  fetchCustomerDetail()
})
</script>

<style scoped>
.customer-detail {
  padding: 20px;
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>